﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Reflection;
namespace Shop
{
    public static class MethodBaseExt
    {
        #region 自动写入错误日志，注意，必须保证根目录中有Log这个文件夹，否则会报错的。
        public static void WriteErrLog(this MethodBase mb, string errMsg,string filename="")
        {
            string cFromIP =Common.HandleLog.GetIPAddress();
            //string cClassName = mb.DeclaringType.FullName;
            //string procName = mb.Name;
            string path = HttpContext.Current.Server.MapPath("~/Log/") + DateTime.Now.ToString("yyyy-MM-dd") + filename + ".log";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
            fs.Position = fs.Length;
            StreamWriter sw = new StreamWriter(fs);
            //sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "|" + errMsg + "|" + cClassName + "|" + procName + "|" + cFromIP);
            sw.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "|" + errMsg+ cFromIP);
            HttpRequest hq = System.Web.HttpContext.Current.Request;            
            sw.Write(hq.RawUrl);
            sw.WriteLine( hq.QueryString.getnvVal_String());
            sw.WriteLine(hq.Form.getnvVal_String());
            sw.WriteLine(Environment.NewLine);
            sw.WriteLine(Environment.NewLine);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }
        #endregion
    }
}